package stack;

import java.util.Stack;

public class MinStack {

    Stack<Integer> stack;
    Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        if (minStack.empty() || val <= minStack.peek()) {
            minStack.push(val);
            stack.push(val);
        } else {
            stack.push(val);
        }
    }

    public void pop() {
        if (stack.empty()) {
            return;
        } else if (stack.peek().equals(minStack.peek())) {
            stack.pop();
            minStack.pop();
        } else {
            stack.pop();
        }
    }

    public int top() {
        if (stack.empty()) {
            return -1;
        } else {
            return stack.peek();
        }
    }

    public int getMin() {
        if (stack.empty()) {
            return -1;
        } else {
            return minStack.peek();
        }
    }
}
